package com.live.dengdengtv.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.aigestudio.wheelpicker.WheelPicker;
import com.live.dengdengtv.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wyf on 2016/8/11.
 */
public class AddressPickerDialog_bak {


    private Dialog pickDialog;
    private Context context;

    private OnWheelAddressDismiss onWheelAddressDismiss;
    private WheelPicker wheel_province;
    private WheelPicker wheel_city;


    private JSONObject mJsonObj;

    private ArrayList<String> provinceList = new ArrayList<String>();
    private Map<String, List<String>> mCitisDatasMap = new HashMap<String, List<String>>();


    public void showDatePickerDialog(Activity activity, OnWheelAddressDismiss onWheelAddressDismiss) {

        context = activity;
        this.onWheelAddressDismiss = onWheelAddressDismiss;

        pickDialog = new Dialog(activity, R.style.dialog_float);
        pickDialog.setContentView(R.layout.dialog_addresspicker);

        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Window dlgwin = pickDialog.getWindow();
        WindowManager.LayoutParams lp = dlgwin.getAttributes();
        dlgwin.setGravity(Gravity.BOTTOM);
        lp.width = (int)(display.getWidth()); //设置宽度

        pickDialog.getWindow().setAttributes(lp);




        initJsonData();
        initDatas();


        wheel_province = (WheelPicker) pickDialog.findViewById(R.id.wheel_province);
        wheel_province.setData(provinceList);

        wheel_city = (WheelPicker) pickDialog.findViewById(R.id.wheel_city);
        wheel_city.setData(mCitisDatasMap.get("北京"));

        wheel_province.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                wheel_city.setData(mCitisDatasMap.get(provinceList.get(position)));

            }
        });



        pickDialog.setOnDismissListener(onDismissListener);
        pickDialog.show();

    }

    /**
     * 从文件中读取地址数据
     */
    private void initJsonData() {
        try {
            StringBuffer sb = new StringBuffer();
            InputStream is = context.getAssets().open("city.json");
            int len = -1;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf)) != -1) {
                sb.append(new String(buf, 0, len, "gbk"));
            }
            is.close();
            mJsonObj = new JSONObject(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    /**
     * 解析数据
     */
    private void initDatas() {
        try {
            JSONArray jsonArray = mJsonObj.getJSONArray("citylist");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonP = jsonArray.getJSONObject(i);
                String province = jsonP.getString("p");

                provinceList.add(province);

                JSONArray jsonCs = null;
                try {
                    /**
                     * Throws JSONException if the mapping doesn't exist or is
                     * not a JSONArray.
                     */
                    jsonCs = jsonP.getJSONArray("c");
                } catch (Exception e1) {
                    continue;
                }
                ArrayList<String> mCitiesDatas = new ArrayList<String>();
                for (int j = 0; j < jsonCs.length(); j++) {
                    JSONObject jsonCity = jsonCs.getJSONObject(j);
                    String city = jsonCity.getString("n");
                    mCitiesDatas.add(city);
                    JSONArray jsonAreas = null;
                    try {
                        /**
                         * Throws JSONException if the mapping doesn't exist or
                         * is not a JSONArray.
                         */
                        jsonAreas = jsonCity.getJSONArray("a");
                    } catch (Exception e) {
                        continue;
                    }

                    String[] mAreasDatas = new String[jsonAreas.length()];
                    for (int k = 0; k < jsonAreas.length(); k++) {
                        String area = jsonAreas.getJSONObject(k).getString("s");
                        mAreasDatas[k] = area;
                    }
                }
                mCitisDatasMap.put(province, mCitiesDatas);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        mJsonObj = null;
    }


    private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {

            int pIndex = wheel_province.getCurrentItemPosition();
            int cIndex = wheel_city.getCurrentItemPosition();
            String p = provinceList.get(pIndex);

            onWheelAddressDismiss.onWheelAddressDismiss(p,mCitisDatasMap.get(p).get(cIndex));
            onWheelAddressDismiss = null;
        }
    };


    public interface OnWheelAddressDismiss {
        void onWheelAddressDismiss(String p, String c);
    }

}
